// std functions
#include <iostream>
#include <stdexcept>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>

// boost functions
#include <boost/program_options.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
// my functions
#include <open_stream_name_with_substitutions.h>
#include <print_error.h>
#include <program_options_helper.h>
#include <stlhelper.h>
#define DEBUG_LEVEL 6
#include <debug_func.h>
#include <blast_results.h>
#include "get_arg.h"
using std::string;
using std::cerr;
using std::cout;
using std::max;
using boost::lexical_cast;




// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp
//-lboost_filesystem-gcc-d
//-L/usr/local/lib

using namespace program_options_helper;
namespace po = boost::program_options_lg;


//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	// database login string
	std::string		dbname;

	//
	// checked mandatory arguments
	//
	po::options_description mandatory_args("Mandatory Arguments");
	mandatory_args.add_options()

	("fasta_file,f",	po::value<string>(&args.fasta_path)
								->set_name("FILE")
								->default_value("(\\S+)"),
							"Sequences in FASTA format.")
	("name_source,n",	po::value<string>(&args.db_origin)
								->set_name("NAME"),
							"Unique identifier for this sequence database.\n"
							"E.g. SWISS")
	("dbname,d", 		po::value<string>(&dbname)
								->set_name("DATABASE"),
							"Existing PostgreSQL database name.");





	// database login string
	std::string		user;
	std::string		host;
	std::string		password;
	std::string		extra_login;

	int default_taxid;

	args.optional_args.add_options()
	("regex,r", 		po::value<string>(&args.regex_str)
								->set_name("REGEX")
								->default_value("(\\S+)"),
							"Regular expression for extracting the sequence identifier. "
							"Defaults to first non-whitespace accession characters.")

	("indices,i", 			"Do not rebuild indices when adding sequences/annotations.")
	("stats,s", 		po::bool_switch(&args.vacuum),
							"Recalibrate database stats for faster queries.")
	("orphans,o", 		po::bool_switch(&args.orphans),
							"Leave obsolete sequences without corresponding accessions ")
	("taxid,t", 		po::value<int>(&default_taxid)
								->set_name("NUMBER"),
							"NCBI Taxonomical ID (overrides any taxa on the accession line.")

	//____________________________________________________________________________________
	// panda login parameters
	("host,H",			po::value<string>(&host)
								->set_name("HOST"),
							"PostgreSQL database host computer address.")
	("user,u",			po::value<string>(&user)
								->set_name("USERNAME")
								->default_value("postgres"),
							"PostgreSQL database user name.")
	("password,p",		po::value<string>(&password)
								->set_name("PASSWORD"),
							"PostgreSQL database password.")
	("login,L",			po::value<string>(&extra_login)
								->set_name("OPTIONS"),
							"Other PostgreSQL login parameters in the form of "
							"keyword = value.\n"
							"E.g \"port=1234\"");
	//____________________________________________________________________________________




	args.add_std_options();
	po::options_description all_args("");
	all_args
						.add(mandatory_args)
						.add(args.optional_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);

	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.2", 80);
		return false;
	}

	if (vm.count("help"))
	{
		string exe_description ="Synchronizes the Panda SQL Database with the "
									"specified sequence file.\n";

		print_usage(cerr, string(*argv), "[OPTIONS] < SEQUENCES.FASTA > ALIGNMENT.RESULTS",
							all_args, exe_description, 80);
		return false;
	}

	check_required_options(vm, mandatory_args);

	args.recreate_indices		= !vm.count("indices");

	//
	// initialize postgresql login
	//
	dbname	= "dbname=" + dbname;
	if (host.length())
		host = "host=" + host;
	if (password.length())
		password = "password=" + password;
	user = "user=" + user;
	args.db_login = dbname + ' ' + host + ' ' + password + ' ' + user + ' ' + extra_login;


	args.fasta_name = get_file_name_substitutions("[FILENAME][EXT]", args.fasta_path);

	if (vm.count("taxid"))
		args.default_taxid = lexical_cast<string>(static_cast<int>(default_taxid));


	args.open_err_log();
	return true;
}

















